package com.peanut.baby.mvp.main;

import android.app.Activity;

import com.peanut.baby.InitManager;
import com.peanut.baby.http.BaseObserver;
import com.peanut.baby.http.BaseResponse;
import com.peanut.baby.http.BaseScheduler;
import com.peanut.baby.http.RetrofitClient;
import com.peanut.baby.model.Advice;
import com.peanut.baby.model.HomeData;
import com.peanut.baby.model.LiveRoom;
import com.peanut.baby.model.QA;

import cn.nekocode.rxlifecycle.RxLifecycle;
import io.reactivex.Observable;

/**
 * Created by anonymouslystudio on 2017/8/28.
 */

public class HomeFragmentPresenter implements HomeFragmentContract.Presenter{
    private HomeFragmentContract.View view;
    private Activity fragment;
    public HomeFragmentPresenter(HomeFragmentContract.View view,Activity fragment) {
        this.view = view;
        this.fragment =fragment;
    }

    @Override
    public void getHomeData() {
        Observable obs = RetrofitClient.getInstance().getHomeData(InitManager.getInstance().getUserId());
        obs.compose(BaseScheduler.<BaseResponse<HomeData>>compose())
                .compose(RxLifecycle.bind(fragment).withObservable())
                .subscribe(new BaseObserver<HomeData>() {
            @Override
            protected void onHandleSuccess(HomeData o) {
                view.onHomeDataGet(o);
            }

            @Override
            protected void onHandleError(String code,String msg) {
                view.onGetHomeDataFailed(msg);
            }
        });
    }

    @Override
    public void getAdvice() {
        Observable obs = RetrofitClient.getInstance().getAdvice(InitManager.getInstance().getUser().userToken,
                InitManager.getInstance().getUserId());
        obs.compose(BaseScheduler.<Advice>compose())
                .compose(RxLifecycle.bind(fragment).withObservable())
                .subscribe(new BaseObserver<Advice>() {
            @Override
            protected void onHandleSuccess(Advice o) {
//                o.userId = Integer.parseInt(InitManager.getInstance().getUserId());
//                o.date = TimeUtil.getCurrentDate();
//                DBManager.getInstance().updateAdviceRecord(o);
                view.onAdviceGet(o);
            }

            @Override
            protected void onHandleError(String code,String msg) {
                view.onGetAdviceFailed(msg);
            }
        });
    }

    @Override
    public void joinRoom(final LiveRoom room) {
        Observable observable = RetrofitClient.getInstance().enroleLiveRoom(InitManager.getInstance().getUser().userToken,
                InitManager.getInstance().getUserId(),room.roomId+"");
        observable.compose(BaseScheduler.<BaseResponse<String>>compose())
                .compose(RxLifecycle.bind(fragment).withObservable())
                .subscribe(new BaseObserver<String>() {
            @Override
            protected void onHandleSuccess(String o) {
                room.isEnroled = 1;
                view.onJoinSuccess(room);

            }

            @Override
            protected void onHandleError(String code,String msg) {
                view.onJoinFailed(code,msg);
            }

        });
    }

    @Override
    public void praiseQA(final QA qa) {
        Observable obs = RetrofitClient.getInstance().praiseQA(qa.id+"", InitManager.getInstance().getUserId(),InitManager.getInstance().getUser().userToken);
        obs.compose(BaseScheduler.<BaseResponse<QA>>compose())
                .compose(RxLifecycle.bind(fragment).withObservable())
                .subscribe(new BaseObserver<QA>() {
            @Override
            protected void onHandleSuccess(QA o) {
                qa.praiseFlag = o.praiseFlag;
                qa.praiseCount = o.praiseCount;
                view.onPraiseSuccess(o);
            }

            @Override
            protected void onHandleError(String code,String msg) {
                view.onPraiseFailed("请求失败 "+msg);
            }

        });
    }
}
